package uiUtil;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import controlLayer.ScheduleCtrl;
import modelLayer.Schedule;
import modelLayer.TeachingBlock;

public class ScheduleBlockPanel extends JPanel {
	
	private Schedule schedule;
	private TeachingBlock tb = new TeachingBlock();
	private ScheduleCtrl sc;
	private ButtonGroup group;
	private JRadioButton selectBlock;
	private JLabel staffLabel = new JLabel();
	private JLabel subjectLabel = new JLabel();
	private JLabel roomLabel = new JLabel("Free block");
	private int blockNo;
	
	public ScheduleBlockPanel (Schedule s, ScheduleCtrl scheduleCtrl, ButtonGroup bg) {
		
		this.schedule = s;
		this.sc = scheduleCtrl;
		this.group = bg;
		this.blockNo = schedule.getBlockNo();
		
		setup();
		
	}
	
	private void setup () {
		
		
		try {
			
			tb = schedule.getTeachingBlock();
			staffLabel.setText(tb.getStaff().getName());
			subjectLabel.setText(tb.getSubject().getName());
			roomLabel.setText(tb.getRoom().getName());
			
//			staff = schedule.getTeachingBlock().getStaff().getName();
//			subject = schedule.getTeachingBlock().getSubject().getName();
//			room = schedule.getTeachingBlock().getRoom().getName();
		}
		catch (Exception e) {
			System.out.println("Exception: " + e);
		}
		
		setLayout(new GridLayout(4,1));
		setBorder(BorderFactory.createLineBorder(Color.black));
		
		selectBlock = new JRadioButton();
		group.add(selectBlock);
		
		selectBlock.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				sc.setSelectedBlockNo(schedule.getBlockNo());
				
			}
		});
		
		add(selectBlock);
		
		add(staffLabel);
		
		add(subjectLabel);
		
		add(roomLabel);
		
	}
	
	public void updateBlock (TeachingBlock tb) {
		
		staffLabel.setText("");
		subjectLabel.setText("");
		roomLabel.setText("Free block");
		
		try{
			staffLabel.setText(tb.getStaff().getName());
			subjectLabel.setText(tb.getSubject().getName());
			roomLabel.setText(tb.getRoom().getName());
		
			schedule.setTeachingBlock(tb);
		
			this.updateUI();
		}
		catch (Exception e) {
			System.out.println("Exception: " + e);
		}
		
	}

	public TeachingBlock getTb() {
		return tb;
	}

	public void setTb(TeachingBlock tb) {
		this.tb = tb;
	}

	public int getBlockNo() {
		return blockNo;
	}

	public void setBlockNo(int blockNo) {
		this.blockNo = blockNo;
	}
	
}
